package com.sl.hx.shop.ui.home.presenter;

import android.content.Context;

import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.ProductList;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.ApiRetrofit;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.contacts.GoodsListContacts;
import com.sl.hx.shop.utils.SPUtils;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public class GoodsListPresenter extends BasePresenter<GoodsListContacts.view> implements GoodsListContacts.presenter {

    private Context mContext;


    public GoodsListPresenter(Context context) {
        this.mContext=context;

    }

    public void getProductLists(int favoritesId, int page, int num) {
        Observable observable = ApiFactory.getInstance().getProductList(ApiRetrofit.getCacheControl(),favoritesId,1,page,num);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<ProductList>>(mContext) {
            @Override
            protected void _onNext(List<ProductList> productLists) {
                List<ProductList> bean=new ArrayList<>();
                for (int i=0;i<productLists.size();i++){
                    if (productLists.get(i).getStatus().equals("1")){
                        bean.add(productLists.get(i));
                    }
                }
                getView().ShowProductList(bean);
            }
            @Override
            protected void _onError(String message) {
            }
        }, false,getView(), FragmentEvent.DESTROY);
    }

    //统计
    public void indexCount(int mId) {
        Observable observable = ApiFactory.getInstance().indexCount(4, SPUtils.getInstance().getInt(SPUKey.TOKENID),mId, AppConfig.CHANNEL);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<String>(mContext) {
            @Override
            protected void _onNext(String s) {
            }
            @Override
            protected void _onError(String message) {

            }
        }, false, getView(), FragmentEvent.DESTROY);
    }
}
